/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package game.scene.map;
import game.GlobalModel;
import java.util.Hashtable;
import java.util.Vector;
import dgo.misc.TextureManager;
import game.scene.map.WindowManager;
import dgo.tilemap.*;
import game.scene.map.event.*;
import game.Config;
import org.json.me.JSONArray;

/**
 *
 * @author Daigo
 */
public class Model {
  public final int showWidth = Config.GAME_SCREEN_WIDTH;
  public final int showHeight = Config.GAME_SCREEN_HEIGHT;
  
  public WindowManager windowManager;
  public Vector tasks;
  public GlobalModel globalModel;
  public Hashtable characterChipsets;
  public final int GRIDSIZE = 16;
  public Vector characters, events;
  public Map map;
  private Hashtable _characterTable;
  public TextureManager textureManager;
  public Model(GlobalModel globalModel, Hashtable options){
    textureManager = new TextureManager();
    this.globalModel = globalModel;
    this.globalModel.variables[12] = -1;
    
    // Set player's initial position
    String mapID;
    int startX, startY;
    if(options.containsKey("map_id")){
      mapID = (String)options.get("map_id");
      startX = Integer.parseInt(options.get("start_x").toString());
      startY = Integer.parseInt(options.get("start_y").toString());
    }else{
      Hashtable startInfo = globalModel.loadStartInfo();
      mapID = (String)startInfo.get("start_map_id");
      startX = Integer.parseInt(startInfo.get("start_x").toString());
      startY = Integer.parseInt(startInfo.get("start_y").toString());
    }
    map = MapLoader.loadMap(getClass(), mapID);
    characters = new Vector();
    _characterTable = new Hashtable();
    characterChipsets = new Hashtable();
    
    Event playerEvent = new Event(0, this, addCharacter(0, startX, startY));
    
    // Creating [["never"]]
    Vector v1 = new Vector();
    v1.addElement("never");
    Vector v2 = new Vector();
    v2.addElement(new JSONArray(v1));
    //
    
    EventParameters ep = (EventParameters)options.get("ep");
    
    if(ep != null){
      playerEvent.push(new EventPage(playerEvent, this, new JSONArray(v2), new JSONArray(), new JSONArray(), new JSONArray(), ep));
      player().setDir(ep.dir);
    }else{
      playerEvent.push(new EventPage(playerEvent, this, new JSONArray(v2), new JSONArray(), new JSONArray(), new JSONArray(), new EventParameters()));
    }
    player().setChip(this.getCharacterChipset("Ghost"), 0, 0);
    player().visible = true;
    events = new Vector();
    events.addElement(playerEvent);
    EventLoader.loadEvents(events, this, mapID);
    //@character_list.sort!{|a, b|a.id <=> b.id}
    windowManager = new WindowManager(globalModel);

    tasks = new Vector();

  }
  
  public MapCharacter player(){
    return (MapCharacter)(_characterTable.get(new Integer(0)));
  }
  
  public MapCharacter addCharacter(int no, int x, int y){
    MapCharacter t =  new MapCharacter(map, null, 0, 0, x, y, true);
    characters.addElement(t);
    _characterTable.put(new Integer(no), t);
    return t;
  }
  public CharacterChipset getCharacterChipset(String id){
    if(id == null){return null;}
    try{
      if(!characterChipsets.contains(id)){
        characterChipsets.put(id, 
          new CharacterChipset(
            id,
            textureManager.getTexture("game/character/" + id),
            Integer.parseInt(globalModel.charasetsSetting.getJSONObject(id).getString("gw")),
            Integer.parseInt(globalModel.charasetsSetting.getJSONObject(id).getString("gh")),
            Byte.parseByte(globalModel.charasetsSetting.getJSONObject(id).getString("anime_num")),
            Byte.parseByte(globalModel.charasetsSetting.getJSONObject(id).getString("dir_num")))
        );
      }
    }catch(Exception e){
      System.out.println("getCharacterChipset: " + e);
    }
        
    return (CharacterChipset)characterChipsets.get(id);
  }
  
  public MapCharacter getCharacter(int id){
    return (MapCharacter)_characterTable.get(new Integer(id));
  }
  
  public Event getEvent(int idx){
    return (Event)events.elementAt(idx);
  }
  
  public Event getEventByID(int id){
      for(int i=0; i < events.size(); i++){
        Event e = (Event)events.elementAt(i);
        if(e.id == id){
          return e;
        }
      }
 
     return null;
  }
}
